package 资本家多态项目;


import java.util.Arrays;

public class Capitalist {
    public void forceToWork(Labor labor){
        labor.work();
    }

}

class Test{
    public static void main(String[] args) {
        Capitalist cap = new Capitalist();
        Labor labor1 = new CowAndHouse();
        Labor labor2 = new Worker();
        Labor labor3 = new Programmer();
        cap.forceToWork(labor1);
        cap.forceToWork(labor2);
        cap.forceToWork(labor3);
        /**使用局部内部类产生一个实现了Labor接口的对象传递给forceToWork方法*/
        class PartialInnerClass implements Labor{
            private String name="沙雕";
            public void playGame(){}
            @Override
            public void work() {
                int[] i = new int[10];
                for (int i1 = 0; i1 < i.length; i1++) {
                    i[i1]=i1*i1;
                }
                System.out.println(Arrays.toString(i));
            }
        }
        PartialInnerClass p = new PartialInnerClass();
        Labor labor5 = new PartialInnerClass();
        cap.forceToWork(labor5);
        /**匿名内部类*/
        cap.forceToWork(new Labor(){
            public void work(){
                System.out.println("把资本家挂路灯上");
            }
        });

        cap.forceToWork(()->{
            System.out.println("Lambda表达式天下无敌");
        });
    }
}

